1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
utils = require('./utils'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A date. |
6
|
|
|
* |
7
|
|
|
* @constructor |
8
|
|
|
* @param {Object} [json] |
|
|
|
|
9
|
|
|
* @alias Date |
10
|
|
|
*/ |
11
|
|
|
var GDate = function(json){ |
12
|
|
|
|
13
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
14
|
|
|
if(!(this instanceof GDate)){ |
15
|
|
|
return new GDate(json); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
19
|
|
|
if(GDate.isInstance(json)){ |
20
|
|
|
return json; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
ExtensibleData.call(this, json); |
24
|
|
|
|
25
|
|
|
if(json){ |
|
|
|
|
26
|
|
|
this.setOriginal(json.original); |
27
|
|
|
this.setFormal(json.formal); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
GDate.prototype = Object.create(ExtensibleData.prototype); |
32
|
|
|
|
33
|
|
|
GDate._gedxClass = GDate.prototype._gedxClass = 'GedcomX.Date'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check whether the given object is an instance of this class. |
37
|
|
|
* |
38
|
|
|
* @param {Object} obj |
39
|
|
|
* @returns {Boolean} |
40
|
|
|
*/ |
41
|
|
|
GDate.isInstance = function(obj){ |
42
|
|
|
return utils.isInstance(obj, this._gedxClass); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the original date value |
47
|
|
|
* |
48
|
|
|
* @returns {String} original |
49
|
|
|
*/ |
50
|
|
|
GDate.prototype.getOriginal = function(){ |
51
|
|
|
return this.original; |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the original date value |
56
|
|
|
* |
57
|
|
|
* @param {String} original |
58
|
|
|
* @returns {Date} This instance. |
59
|
|
|
*/ |
60
|
|
|
GDate.prototype.setOriginal = function(original){ |
61
|
|
|
this.original = original; |
62
|
|
|
return this; |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the formal date value |
67
|
|
|
* |
68
|
|
|
* @returns {String} |
69
|
|
|
*/ |
70
|
|
|
GDate.prototype.getFormal = function(){ |
71
|
|
|
return this.formal; |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the formal date value |
76
|
|
|
* |
77
|
|
|
* @param {String} formal |
78
|
|
|
* @returns {Date} This instance. |
79
|
|
|
*/ |
80
|
|
|
GDate.prototype.setFormal = function(formal){ |
81
|
|
|
this.formal = formal; |
82
|
|
|
return this; |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Export the object as JSON |
87
|
|
|
* |
88
|
|
|
* @return {Object} JSON object |
89
|
|
|
*/ |
90
|
|
|
GDate.prototype.toJSON = function(){ |
91
|
|
|
var json = ExtensibleData.prototype.toJSON.call(this); |
92
|
|
|
|
93
|
|
|
if(this.original){ |
94
|
|
|
json.original = this.original; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if(this.formal){ |
98
|
|
|
json.formal = this.formal; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return json; |
102
|
|
|
}; |
103
|
|
|
|
104
|
|
|
module.exports = GDate; |